<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Admin extends MX_Controller {

    function __construct(){
        parent::__construct();
        $this->init();
        $this->template->set_template('admin');
    }
    
    public function init(){
        if(!$this->ion_auth->is_admin()){
            redirect('', 'location');  
        }
    }
    function index(){  
        $data = array();
        $this->template->parse_view('content' , 'admin/admin', $data);
        $this->template->render();        
    }
    
    public function user($param){
        
        $this->template->add_js('assets/scripts/admin/users/admin_user.js');
        $this->load->model('admin_user_model');  
        $data = array();
        $data['message'] = '';
        if(!$param){
            redirect('admin/user/index' , 'location');
        }
        switch ($param){
            case 'index': 
                if(isset($_REQUEST['page'])){
                    $start = $_REQUEST['page'] > 0 ? ($_REQUEST['page']-1)*1 : 0; 
                }
                else{
                    $start = 0;
                }
                
                $this->load->library('pagination');
               
                $result =  $this->admin_user_model->get_list_user($start , $limit = 2);           
                $data['users'] = $result['rows'];
                
                $config['base_url'] = BASE_URL_ADMIN .'user/index?page=';
                
                $config['base_url'] = 'http://example.com/index.php/test/page/';
$config['total_rows'] = 200;
$config['per_page'] = 20; 

                $this->pagination->initialize($config); 
                $data['pagination'] = $this->pagination->create_links();
                
                $this->template->parse_view('content' , 'admin/users/index', $data);
                $this->template->render();   
                break;
            
            case 'edit':    
                $uid = $_REQUEST['id'];
                if(isset($_POST) && !empty($_POST)){ 
                    $data_update = array(
                                        'full_name' => $this->input->post('full_name'),
                                        'email'  => $this->input->post('email'),				
                                        'phone'      => $this->input->post('phone'),
                                );
                    $this->ion_auth->update($uid , $data_update);
                    $this->session->set_flashdata('message', "Lưu thành công");
                    $data['message'] = "Lưu thành công";
                }                       
                $iuser = $this->ion_auth->user( $uid )->row_array();
                $data['iuser']  = $iuser;               
                $this->template->parse_view('content' , 'admin/users/edit', $data);
                $this->template->render(); 
                break;
                
            case 'delete':
                $uid = $_REQUEST['id'];
                if($this->ion_auth->delete_user( $uid )){
                    $data['message'] = 'Xóa thành công';
                }
                else{
                    $data['message'] = 'Xóa không thành công';
                }
                redirect($_SERVER['HTTP_REFERER'] , 'location');
                break;
                
            case 'lock':
                $uid = $_REQUEST['id'];
                if($this->ion_auth->deactivate( $uid )){
                    $data['message'] = 'Khóa thành công';
                }
                else{
                    $data['message'] = 'Khóa không thành công';
                }
                redirect($_SERVER['HTTP_REFERER'] , 'location');
                break;
            case 'active':
                $uid = $_REQUEST['id'];
                if($this->ion_auth->activate( $uid , FALSE)){
                    $data['message'] = 'Mở khóa thành công';
                }
                else{
                    $data['message'] = 'Mở khóa không thành công';
                }
                redirect($_SERVER['HTTP_REFERER'] , 'location');
                break;
            default :
                $list_user = $this->admin_user_model->get_list_user($start = 0 , $limit = 20);
                $data['users'] = $list_user;
                $this->template->parse_view('content' , 'admin/users/index', $data);
                $this->template->render();   
                break;
                break;
        }
    }
    
    
    
    
/* ================================================

    END CLASS
    
    
=================================================== */
}